Dynamic PDF age is valuable when you need to enable the client to download the content or HTML content in a file on the web application. For this situation, the HTML content should be changed over to the PDF record before download. You can Convert HTML to PDF in PHP with Dompdf. Dompdf is the most straightforward approach to make PDF file with dynamic information using PHP. Dompdf library help to Generate a PDF document and add the HTML content to PDF using PHP.
Dompdf gives different configuration option to upgrade the PDF generation functionality. Adding Watermark to PDF is a standout helpful functionality among them. In this article tutorial, we will tell you the best way to change over HTML to PDF and add watermark to PDF with Dompdf using PHP.
A watermark is a picture or text that can show up either front or behind the content of the PDF file. In the model code, we will explain the both methods to add text and picture watermark to PDF report with Dompdf in PHP.
In this Article i will show you all the process step by step:
Step-1:
Download the Zip version of dompdf (stable release) from GitHub. Extract the Dompdf package and upload it in the directory of your application.
Step-2:
Include Dompdf autoloader to load dompdf libraries and helper functions in the PHP script.
2 3 4 5 |
// Include autoloader require_once 'dompdf/autoload.inc.php'; |
First we will add Text Watermark to PDF
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?php // Include autoloader require_once 'dompdf/autoload.inc.php'; // Reference the Dompdf namespace use Dompdf\Dompdf; // Reference the Options namespace use Dompdf\Options; // Reference the Font Metrics namespace use Dompdf\FontMetrics; // Set options to enable embedded PHP $options = new Options(); $options->set('isPhpEnabled', 'true'); // Instantiate dompdf class $dompdf = new Dompdf($options); // Load HTML content $dompdf->loadHtml('<img class="alignleft" src="./tutorials-website-logo.png" alt="Pradeep Maurya" width="100%"><center><h1>Welcome to Tutorials Website</h1><div class="wpb_wrapper"> <p><strong>Tutorialswebsite</strong> is a leading online education portal that helps technologies, software, business and creative skills readers to learn new skills at their own place from the comforts of their drawing rooms. Individual, corporate and academic members have access to learn anything on <a href="https://www.tutorialswebsite.com">tutorialswebsite.com</a> likes video tutorials, blogs content etc.</p> <p>From 5 years, we worked our way to adding new fresh articles on topics ranging from programming languages that helps students, leaders, IT and design professionals, project managers or anyone who is working with software development, creatives and business skills.</p> <h2 style="text-align: center;">Mission</h2> <p>Our mission is to deliver easy and best online resources on programming and web development to encourage our readers acquire as many skills as they would like to. We offer the useful and best tutorials for web professionals-developers, programmers, freelancer free of cost. We don’t force our readers to sign up with us or submit their details.</p> </div><h2 style="font-size: 24px;text-align: center;font-family:Roboto;font-weight:700;font-style:normal" class="vc_custom_heading">About Author</h2> <p> <strong><a href="https://www.pradeepmaurya.in/">Pradeep Maurya</a></strong> is the Professional Web Developer and Founder of <a href="https://www.tutorialswebsite.com">“Tutorialswebsite”</a>. He lives in Delhi and loves to be a self dependent person. As an author, he is trying his best to improve this platform day by day. You can contact him <strong><a href="https://www.facebook.com/erpradeepmauryarkt" target="_blank" rel="noopener noreferrer">@facebook</a></strong><strong>Website:</strong> <a href="https://www.pradeepmaurya.in" target="_blank" rel="noopener noreferrer">https://www.pradeepmaurya.in</a></p></center> '); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', ''); // Render the HTML as PDF $dompdf->render(); // Instantiate canvas instance $canvas = $dompdf->getCanvas(); // Instantiate font metrics class $fontMetrics = new FontMetrics($canvas, $options); // Get height and width of page $w = $canvas->get_width(); $h = $canvas->get_height(); // Get font family file $font = $fontMetrics->getFont('times'); // Specify watermark text $text = "https://www.tutorialswebsite.com"; // Get height and width of text $txtHeight = $fontMetrics->getFontHeight($font, 150); $textWidth = $fontMetrics->getTextWidth($text, $font, 40); // Set text opacity $canvas->set_opacity(.2); // Specify horizontal and vertical position $x = (($w-$textWidth)/2); $y = (($h-$txtHeight)/2); /** * Writes text at the specified x and y coordinates. * * @param float $x * @param float $y * @param string $text the text to write * @param string $font the font file to use * @param float $size the font size, in points * @param array $color * @param float $word_space word spacing adjustment * @param float $char_space char spacing adjustment * @param float $angle angle */ // Writes text at the specified x and y coordinates $canvas->text($x, $y, $text, $font, 40,$color = array(255,0,0), $word_space = 0.0, $char_space = 0.0, $angle = 20.0); // Output the generated PDF (1 = download and 0 = preview) $dompdf->stream('document.pdf', array("Attachment" => 0)); |
Add Image Watermark to PDF
The following code generates image watermark PDF with Dompdf library using PHP. The process is the same as like the text watermark code (above) except the image setup.
- Define the image path which you want to add a watermark. Please upload image in same directory or folder.
- Add watermark image to PDF document using image() method of Canvas class.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?php // Include autoloader require_once 'dompdf/autoload.inc.php'; // Reference the Dompdf namespace use Dompdf\Dompdf; // Reference the Options namespace use Dompdf\Options; // Set options to enable embedded PHP $options = new Options(); $options->set('isPhpEnabled', 'true'); // Instantiate dompdf class $dompdf = new Dompdf($options); // Load HTML content $dompdf->loadHtml('<h3>https://www.TutorialsWebsite.com</h3><center><h1>Welcome to Tutorials Website</h1><div class="wpb_wrapper"> <p><strong>Tutorialswebsite</strong> is a leading online education portal that helps technologies, software, business and creative skills readers to learn new skills at their own place from the comforts of their drawing rooms. Individual, corporate and academic members have access to learn anything on <a href="https://www.tutorialswebsite.com">tutorialswebsite.com</a> likes video tutorials, blogs content etc.</p> <p>From 5 years, we worked our way to adding new fresh articles on topics ranging from programming languages that helps students, leaders, IT and design professionals, project managers or anyone who is working with software development, creatives and business skills.</p> <h2 style="text-align: center;">Mission</h2> <p>Our mission is to deliver easy and best online resources on programming and web development to encourage our readers acquire as many skills as they would like to. We offer the useful and best tutorials for web professionals-developers, programmers, freelancer free of cost. We don’t force our readers to sign up with us or submit their details.</p> </div><h2 style="font-size: 24px;text-align: center;font-family:Roboto;font-weight:700;font-style:normal" class="vc_custom_heading">About Author</h2> <p> <strong><a href="https://www.pradeepmaurya.in/">Pradeep Maurya</a></strong> is the Professional Web Developer and Founder of <a href="https://www.tutorialswebsite.com">“Tutorialswebsite”</a>. He lives in Delhi and loves to be a self dependent person. As an author, he is trying his best to improve this platform day by day. You can contact him <strong><a href="https://www.facebook.com/erpradeepmauryarkt" target="_blank" rel="noopener noreferrer">@facebook</a></strong><strong>Website:</strong> <a href="https://www.pradeepmaurya.in" target="_blank" rel="noopener noreferrer">https://www.pradeepmaurya.in</a></p></center> '); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', ''); // Render the HTML as PDF $dompdf->render(); // Instantiate canvas instance $canvas = $dompdf->getCanvas(); // Get height and width of page $w = $canvas->get_width(); $h = $canvas->get_height(); // Specify watermark image $imageURL = 'tutorials-website-logo.png'; $imgWidth = 500; $imgHeight = 250; // Set image opacity $canvas->set_opacity(.5); // Specify horizontal and vertical position $x = (($w-$imgWidth)/2); $y = (($h-$imgHeight)/3); // Add an image to the pdf /** * Add an image to the pdf. * * The image is placed at the specified x and y coordinates with the * given width and height. * * @param string $img_url the path to the image * @param float $x x position * @param float $y y position * @param int $w width (in pixels) * @param int $h height (in pixels) * @param string $resolution The resolution of the image */ $canvas->image($imageURL, $x, $y, $imgWidth, $imgHeight,$resolution = "normal"); // Output the generated PDF (1 = download and 0 = preview) $dompdf->stream('document.pdf', array("Attachment" => 0)); |
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
How to Convert HTML to PDF in CodeIgniter using Dompdf Library
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co